package com.mantru.iotpot.Cleaning;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Cleaning_List;
import com.mantru.iotpot.Home.HomeActivity;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Tools.LoadingDialogUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CleanWayActivity extends AppCompatActivity implements View.OnClickListener {

    private ApplicationClass ac;
    private TextView name1;
    private TextView name2;
    private TextView name3;
    private TextView name4;
    private TextView time1;
    private TextView time2;
    private TextView time3;
    private TextView time4;
    private TextView water1;
    private TextView water2;
    private TextView water3;
    private TextView water4;
    private TextView hot1;
    private TextView hot2;
    private TextView hot3;
    private TextView hot4;
    private Button back;
    private List<Cleaning_List> cleaningLists = new ArrayList<>();
    private RelativeLayout button1;
    private RelativeLayout button2;
    private RelativeLayout button3;
    private RelativeLayout button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main11);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        initialization();//初始化按钮
        Log.d("cleaning","开始解析json数据");
        getJson();//获取json数据
        Log.d("cleaning","解析json数据完成");
        setview();//设置按钮文本
    }

    private void initialization(){//初始化按钮
        name1 = findViewById(R.id.wash_name1);
        name2 = findViewById(R.id.wash_name2);
        name3= findViewById(R.id.wash_name3);
        name4 = findViewById(R.id.wash_name4);
        time1 = findViewById(R.id.wash_time1);
        time2 = findViewById(R.id.wash_time2);
        time3 = findViewById(R.id.wash_time3);
        time4 = findViewById(R.id.wash_time4);
        water1 = findViewById(R.id.wash_water1);
        water2 = findViewById(R.id.wash_water2);
        water3 = findViewById(R.id.wash_water3);
        water4 = findViewById(R.id.wash_water4);
        hot1 = findViewById(R.id.wash_hot1);
        hot2 = findViewById(R.id.wash_hot2);
        hot3 = findViewById(R.id.wash_hot3);
        hot4 = findViewById(R.id.wash_hot4);
        back = findViewById(R.id.wash_back);
        button1 = findViewById(R.id.wash_button1);
        button2 = findViewById(R.id.wash_button2);
        button3 = findViewById(R.id.wash_button3);
        button4 = findViewById(R.id.wash_button4);
        back.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    //获取json数据
    public void getJson(){
        String json_data = ac.GetSQLData_Cleaning_List();
        Log.d("cleaning","json_data: " + json_data);
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            boolean success = jsonObject.getBoolean("success");
            if (success){
                Log.d("cleaning","success: " + success);
                JSONArray itemDataArray = jsonObject.getJSONArray("item_data");
                for (int i = 0; i < itemDataArray.length(); i++){
                    JSONObject cleaningJson = itemDataArray.getJSONObject(i);
                    String type = cleaningJson.getString("type");
                    Log.d("cleaning","type--> " + type);
                    String name = cleaningJson.getString("name");
                    Log.d("cleaning","name--> " + name);
                    String time = cleaningJson.getString("time");
                    Log.d("cleaning","time--> " + time);
                    String water = cleaningJson.getString("water");
                    Log.d("cleaning","water--> " + water);
                    String hot = cleaningJson.getString("hot");
                    Log.d("cleaning","hot--> " + hot);
                    cleaningLists.add(new Cleaning_List(type,name,time,water,hot));
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            Log.d("cleaning","解析json数据失败");
        }
    }

    //设置按钮文本
    public void setview() {
        if (!cleaningLists.isEmpty()) {
            for (Cleaning_List clean : cleaningLists) {
                if (clean.getCleaning_type().equals("1")) {
                    name1.setText(clean.getCleaning_name());
                    time1.setText(clean.getCleaning_time() + "s");
                    water1.setText(clean.getCleaning_water() + "g水");
                    if (clean.getCleaning_hot().equals("0")){
                        hot1.setText("不加热");
                    }else {
                        hot1.setText("加热" + clean.getCleaning_hot() + "s");
                    }
                } else if (clean.getCleaning_type().equals("2")) {
                    name2.setText(clean.getCleaning_name());
                    time2.setText(clean.getCleaning_time() + "s");
                    water2.setText(clean.getCleaning_water() + "g水");
                    if (clean.getCleaning_hot().equals("0")){
                        hot2.setText("不加热");
                    }else {
                        hot2.setText("加热" + clean.getCleaning_hot() + "s");
                    }
                } else if (clean.getCleaning_type().equals("3")) {
                    name3.setText(clean.getCleaning_name());
                    time3.setText(clean.getCleaning_time() + "s");
                    water3.setText(clean.getCleaning_water() + "g水");
                    if (clean.getCleaning_hot().equals("0")){
                        hot3.setText("不加热");
                    }else {
                        hot3.setText("加热" + clean.getCleaning_hot() + "s");
                    }
                } else if (clean.getCleaning_type().equals("4")) {
                    name4.setText(clean.getCleaning_name());
                    time4.setText(clean.getCleaning_time() + "s");
                    water4.setText(clean.getCleaning_water() + "g水");
                    if (clean.getCleaning_hot().equals("0")){
                        hot4.setText("不加热");
                    }else {
                        hot4.setText("加热" + clean.getCleaning_hot() + "s");
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.wash_back){
//            LoadingDialogUtil.showLoadingDialog(CleanWayActivity.this);
            finish();
        }else if (v.getId() == R.id.wash_button1) {
            resetButtons();//移除所有按钮的样式
            button1.setBackgroundResource(R.drawable.button12);
//            LoadingDialogUtil.showLoadingDialog(CleanWayActivity.this);
            Intent intent = new Intent(this, CleaningActivity.class);
            intent.putExtra("type", 1);
            startActivity(intent);
            finish();
        }else if (v.getId() == R.id.wash_button2) {
            resetButtons();//移除所有按钮的样式
            button2.setBackgroundResource(R.drawable.button12);
//            LoadingDialogUtil.showLoadingDialog(CleanWayActivity.this);
            Intent intent = new Intent(this, CleaningActivity.class);
            intent.putExtra("type", 2);
            startActivity(intent);
            finish();
        }else if (v.getId() == R.id.wash_button3) {
            resetButtons();//移除所有按钮的样式
            button3.setBackgroundResource(R.drawable.button12);
//            LoadingDialogUtil.showLoadingDialog(CleanWayActivity.this);
            Intent intent = new Intent(this, CleaningActivity.class);
            intent.putExtra("type", 3);
            startActivity(intent);
            finish();
        }else if (v.getId() == R.id.wash_button4) {
            resetButtons();//移除所有按钮的样式
            button4.setBackgroundResource(R.drawable.button12);
//            LoadingDialogUtil.showLoadingDialog(CleanWayActivity.this);
            Intent intent = new Intent(this, CleaningActivity.class);
            intent.putExtra("type", 4);
            startActivity(intent);
            finish();
        }
    }

    private void resetButtons() {//移除所有按钮的样式
        button1.setBackgroundResource(R.drawable.box);
        button2.setBackgroundResource(R.drawable.box);
        button3.setBackgroundResource(R.drawable.box);
        button4.setBackgroundResource(R.drawable.box);
    }

//    private BroadcastReceiver receiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if ("ACTION_CLOSE_LOADING_DIALOG".equals(intent.getAction())) {
//                LoadingDialogUtil.closeLoadingDialog();
//            }
//        }
//    };

    @Override
    protected void onStart() {
        super.onStart();
//        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("ACTION_CLOSE_LOADING_DIALOG"));
    }

    @Override
    protected void onStop() {
        super.onStop();
//        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        // 在界面渲染完成后发送广播关闭弹窗
//        new Handler().post(new Runnable() {
//            @Override
//            public void run() {
//                ((ApplicationClass) getApplication()).sendBroadcast("ACTION_CLOSE_LOADING_DIALOG");
//            }
//        });
    }
}